package Server;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

import Server.BusinessObjects.*;

@Path("/question")
public class QuestionResource {

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{category}/{severity}")
	public String getQuestion(@PathParam("category") int categoryId,
			@PathParam("severity") int severity) {

		JSONObject o = new JSONObject();
		List<JSONObject> objects = new ArrayList<JSONObject>();
		List<Question> qList = new ArrayList<Question>();

		try {
			List<Question> qListFromDB = Question.getQuestionsByCatAndSeverity(
					categoryId, severity);

			
			Random rn = new Random();
			Question q;
			int[] randomQNr;
			
			System.out.println(qListFromDB.size());
			System.out.println("aa");
			
			if(Config.maxQuestionsInPool >= qListFromDB.size()) {
				randomQNr = new int[Config.maxQuestionsInPool];
			} else {
				randomQNr = new int[qListFromDB.size()];
			}
			

			for (int i = 0; i < randomQNr.length; i++) {
				randomQNr[i] = rn.nextInt((qListFromDB.size() - 1) + 1);
				System.out.println(randomQNr[i]);
			}

			for (int i = 0; i < Config.maxQuestionsInPool; i++) {
				q = qListFromDB.get(randomQNr[i]);

				if (!qList.contains(q))
					qList.add(q);

			}

			for (int i = 0; i < qList.size(); i++) {
				o = new JSONObject();
				o.put("Id", qList.get(i).getId());
				o.put("Name", qList.get(i).getName());
				o.put("Option1", qList.get(i).getOption1());
				o.put("Option2", qList.get(i).getOption2());
				o.put("Option3", qList.get(i).getOption3());
				o.put("Option4", qList.get(i).getOption4());
				o.put("Severity", qList.get(i).getSeverity());

				objects.add(o);

				System.out.println(String.format(
						"Send Question %s %s Answer %s", qList.get(i).getId(),
						qList.get(i).getName(), qList.get(i)
								.getRightOptionNumber()));
			} 
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return objects.toString();
	}

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/{category}/")
	public String getQuestion(@PathParam("category") int categoryId) {

		JSONObject o = new JSONObject();
		List<JSONObject> objects = new ArrayList<JSONObject>();
		List<Question> qList = new ArrayList<Question>();

		try {
			List<Question> qListFromDB = Question.getQuestionsByCat(categoryId);

			
			Random rn = new Random();
			Question q;
			int[] randomQNr;
			
			if(Config.maxQuestionsInPool >= qListFromDB.size()) {
				randomQNr = new int[Config.maxQuestionsInPool];
			} else {
				randomQNr = new int[qListFromDB.size()];
			}
			

			for (int i = 0; i < randomQNr.length; i++) {
				randomQNr[i] = rn.nextInt((qListFromDB.size() - 1) + 1);
				System.out.println(randomQNr[i]);
			}

			for (int i = 0; i < randomQNr.length; i++) {
				q = qListFromDB.get(randomQNr[i]);

				if (!qList.contains(q))
					qList.add(q);

			}
			
			for (int i = 0; i < qList.size(); i++) {
				o = new JSONObject();
				o.put("Id", qList.get(i).getId());
				o.put("Name", qList.get(i).getName());
				o.put("Option1", qList.get(i).getOption1());
				o.put("Option2", qList.get(i).getOption2());
				o.put("Option3", qList.get(i).getOption3());
				o.put("Option4", qList.get(i).getOption4());
				o.put("Severity", qList.get(i).getSeverity());

				objects.add(o);

				System.out.println(String.format(
						"Send Question %s %s Answer %s", qList.get(i).getId(),
						qList.get(i).getName(), qList.get(i)
								.getRightOptionNumber()));
			} 		

			/*
			for (int i = 0; i < qListFromDB.size(); i++) {
				o = new JSONObject();
				o.put("Id", qListFromDB.get(i).getId());
				o.put("Name", qListFromDB.get(i).getName());
				o.put("Option1", qListFromDB.get(i).getOption1());
				o.put("Option2", qListFromDB.get(i).getOption2());
				o.put("Option3", qListFromDB.get(i).getOption3());
				o.put("Option4", qListFromDB.get(i).getOption4());
				o.put("Severity", qListFromDB.get(i).getSeverity());

				objects.add(o);

				System.out.println(String.format(
						"Send Question %s %s Answer %s", qListFromDB.get(i).getId(),
						qListFromDB.get(i).getName(), qListFromDB.get(i)
								.getRightOptionNumber()));
			} 
			*/
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return objects.toString();
	}

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/getAllQuestionsWithCategories")
	public String getAllQuestionsWithCategories() {

		List<JSONObject> objects = new ArrayList<JSONObject>();
		try {
			List<Question> questions = Question.getAllQuestion();

			for (Question q : questions) {
				JSONObject o = new JSONObject();
				o.put("QuestionId", q.getId());
				o.put("QuestionName", q.getName());
				o.put("Severity", q.getSeverity());
				o.put("Option1", q.getOption1());
				o.put("Option2", q.getOption2());
				o.put("Option3", q.getOption3());
				o.put("Option4", q.getOption4());
				o.put("RightOptionNumber", q.getRightOptionNumber());
				
				JSONArray catArray = new JSONArray();
				List<Category> categories = Question.getCategoryToQuestion(q
						.getId());
				int i = 1;
				for (Category c : categories) {
					JSONObject co = new JSONObject();
					co.put("CategoryId", c.getId());
					co.put("CategoryName", c.getName());
					catArray.put(co);
					i++;
				}
				o.put("Categories", catArray);
				objects.add(o);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return objects.toString();
	}
}